#!/bin/bash

inputs_dir=/mnt/sdb
outputs_dir=/mnt/sdc
bdb_dir=/mnt/sdd

mkdir ${inputs_dir}
mkfs -t ext3 -F /dev/sdc; mkdir ${outputs_dir}; mount /dev/sdc ${outputs_dir}
mkfs -t ext3 -F /dev/sdd; mkdir ${bdb_dir}; mount /dev/sdd ${bdb_dir}

cp -r /home/data/input ${inputs_dir}
cp -r /home/data/historical ${inputs_dir}
cp -r /home/data/output ${outputs_dir}

input_files=${inputs_dir}/input/*
historical_file=${inputs_dir}/historical/*
output_files=${outputs_dir}/output/*

for zipped_file in ${input_files} ${historical_file} ${output_files}
do bzip2 -d ${zipped_file}; done

# Generate new data files for this replication group by walking over all files,
# and by modifying some fields in these files in accordance with a replication
# group index.
group_index=`GET http://169.254.169.254/latest/meta-data/ami-launch-index`

if [ ${group_index} -ne 0 ]; then

  input_files=${inputs_dir}/input/*
  for input_file in ${input_files}
  do 
    new_file=`perl generate_new_file.pl ${input_file} ${group_index}`
    rm ${input_file}
  done

  output_files=${outputs_dir}/output/*
  for output_file in ${output_files}
  do
    new_file=`perl generate_new_file.pl ${output_file} ${group_index}`
    sort ${new_file} --output=${new_file}.tmp
    mv -f ${new_file}.tmp ${new_file}
    rm ${output_file}
  done

fi
